{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7767eaa5",
   "metadata": {},
   "source": [
    "### 本周学习内容\n",
    "#### 字典的一般方法\n",
    "###### 1、如何查询字典 2、通过字典的方法实现字典的修改（如：增加、删除、修改）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ccee5317",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> \n",
      " {'stu1801': 'Jinshan', 'stu1802': 'Lixin', 'stu1803': 'Jiaxuan'}\n"
     ]
    }
   ],
   "source": [
    "# 新建字典 dict name = { key1:value1,key2:value2}\n",
    "stu_info = {\n",
    "    \"stu1801\":\"Jinshan\",\n",
    "    \"stu1802\":\"Lixin\",\n",
    "    \"stu1803\":\"Jiaxuan\"\n",
    "}\n",
    "print(type(stu_info),'\\n',stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "345dc211",
   "metadata": {},
   "source": [
    ">1.字典的新增（回顾）\n",
    " dict_name(key) = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "62b863d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info[\"stu1804\"] = \"xiaoming\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "55bf15c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'Jinshan', 'stu1802': 'Lixin', 'stu1803': 'Jiaxuan', 'stu1804': 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "63ccf217",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 需要注意的事情\n",
    "###如果字典新增的内容key已经存在，=====》结论，会对已有的key对应的value进行修改\n",
    "stu_info['stu1802'] = \"xiaohong\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e4583494",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'Jinshan', 'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan', 'stu1804': 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4792e218",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 2.字典的删除\n",
    ">>1.字典删除pop（）的一般使用方法 1、先查询要删除的可以是否存在（避免error的报错）： 用in去查询成员（key）是否存在\n",
    "2、然后删除： D.pop(k[,d]) -> v\n",
    ">>2.字典popitem（）的使用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "57427ef4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b13cdac9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jinshan'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.pop(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "31e457fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan', 'stu1804': 'xiaoming'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0e08823d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 如果key不存在会怎么样  ===== KeyError: 'stu1801'不存在我们的字典当中#\n",
    "# stu_info.pop(\"stu1801\")\n",
    "if \"stu1801\" in stu_info:\n",
    "    stu_info.pop(stu1801)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "169974e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan', 'stu1804': 'xiaoming'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0fb6a02c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "####help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ed22fb02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1804', 'xiaoming')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6c0025a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32369c33",
   "metadata": {},
   "source": [
    "## 3.字典的修改\n",
    "A、先查询key是否存在（使用方法：in& not in\n",
    "（1、key存在，将对其修改；1、key不存在，将新增该成员（对应字典的新增）\n",
    "B、dict_name[key]=value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6d31fd67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8f495b47",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info['stu1802'] = 'xiaohong'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "432763ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'xiaohong', 'stu1803': 'Jiaxuan'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a883dba",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 2,字典的实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "004c4911",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input(\"请输入要查询的文本\")\n",
    "# 给定元音字母列表\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "found = {}\n",
    "\n",
    "#found[\"a\"]=0\n",
    "#found[\"e\"]=0\n",
    "#found[\"i\"]=0\n",
    "#found[\"o\"]=0\n",
    "#found[\"u\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    found[i]=0\n",
    "    \n",
    "for i in word:\n",
    "    # I：aeiou每次遍历一个\n",
    "    if i in vowels:\n",
    "        found[i] +=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c91a037",
   "metadata": {},
   "outputs": [],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7686927d",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "found = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d093a5a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.fromkeys(vowels,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d931c281",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e6f5024",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 字典的实践（2）\n",
    "1、setdefault（）的方法\n",
    "2、sorted（）排序"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
